﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000306 RID: 774
	internal class SkipWhileObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x0600104B RID: 4171 RVA: 0x0004B46D File Offset: 0x0004986D
		public SkipWhileObservable(IObservable<T> source, Func<T, bool> predicate) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicate = predicate;
		}

		// Token: 0x0600104C RID: 4172 RVA: 0x0004B489 File Offset: 0x00049889
		public SkipWhileObservable(IObservable<T> source, Func<T, int, bool> predicateWithIndex) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicateWithIndex = predicateWithIndex;
		}

		// Token: 0x0600104D RID: 4173 RVA: 0x0004B4A5 File Offset: 0x000498A5
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.predicate != null)
			{
				return new SkipWhileObservable<T>.SkipWhile(this, observer, cancel).Run();
			}
			return new SkipWhileObservable<T>.SkipWhile_(this, observer, cancel).Run();
		}

		// Token: 0x0400098F RID: 2447
		private readonly IObservable<T> source;

		// Token: 0x04000990 RID: 2448
		private readonly Func<T, bool> predicate;

		// Token: 0x04000991 RID: 2449
		private readonly Func<T, int, bool> predicateWithIndex;

		// Token: 0x02000307 RID: 775
		private class SkipWhile : OperatorObserverBase<T, T>
		{
			// Token: 0x0600104E RID: 4174 RVA: 0x0004B4CD File Offset: 0x000498CD
			public SkipWhile(SkipWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x0600104F RID: 4175 RVA: 0x0004B4DE File Offset: 0x000498DE
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06001050 RID: 4176 RVA: 0x0004B4F4 File Offset: 0x000498F4
			public override void OnNext(T value)
			{
				if (!this.endSkip)
				{
					try
					{
						this.endSkip = !this.parent.predicate(value);
					}
					catch (Exception error)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
						return;
					}
					if (!this.endSkip)
					{
						return;
					}
				}
				this.observer.OnNext(value);
			}

			// Token: 0x06001051 RID: 4177 RVA: 0x0004B584 File Offset: 0x00049984
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001052 RID: 4178 RVA: 0x0004B5BC File Offset: 0x000499BC
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000992 RID: 2450
			private readonly SkipWhileObservable<T> parent;

			// Token: 0x04000993 RID: 2451
			private bool endSkip;
		}

		// Token: 0x02000308 RID: 776
		private class SkipWhile_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06001053 RID: 4179 RVA: 0x0004B5F4 File Offset: 0x000499F4
			public SkipWhile_(SkipWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001054 RID: 4180 RVA: 0x0004B605 File Offset: 0x00049A05
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06001055 RID: 4181 RVA: 0x0004B618 File Offset: 0x00049A18
			public override void OnNext(T value)
			{
				if (!this.endSkip)
				{
					try
					{
						this.endSkip = !this.parent.predicateWithIndex(value, this.index++);
					}
					catch (Exception error)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
						return;
					}
					if (!this.endSkip)
					{
						return;
					}
				}
				this.observer.OnNext(value);
			}

			// Token: 0x06001056 RID: 4182 RVA: 0x0004B6B8 File Offset: 0x00049AB8
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001057 RID: 4183 RVA: 0x0004B6F0 File Offset: 0x00049AF0
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000994 RID: 2452
			private readonly SkipWhileObservable<T> parent;

			// Token: 0x04000995 RID: 2453
			private bool endSkip;

			// Token: 0x04000996 RID: 2454
			private int index;
		}
	}
}
